package com.j180.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public final class SqlSessionUtil {

    private static SqlSessionFactory sqlSessionFactory=null;

    private SqlSessionUtil(){

    };

    static{
        //配置文件所在的地址
        String resource = "config/mybatis-config.xml";
        InputStream inputStream = null;
        // 读取配置文件
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建sql的回话工厂（理解数据库创建连接对象的工厂）
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }
    /**
     * 获得连接
     * @return
     */
   public static SqlSession getSqlSession(){
      return  sqlSessionFactory.openSession();
    }

}
